import RequestCommon from './request-common'
const baseUrl = 'https://wxapp.hnyxkc.com'
function getReqHeader () {
  return {
    Authorization: `Bearer ${wx.getStorageSync('access_token')}`,
    // 'content-type': 'application/json' // 默认值
    Accept: 'application/json',
    'meedu-platform': 'MINI'
  }
}
let requestCommon = new RequestCommon({
  axios(config) {
    return new Promise(function (resolve, reject) {
      let { data: beforeData, method, timeout = 6000 } = config

      // 去掉 undefined
      let data = {}
      if (beforeData) {
        Object.keys(beforeData).forEach(k => {
          let v = beforeData[k]
          if (v !== undefined) {
            data[k] = v
          }
        })
      }

      wx.request({
        url: baseUrl + config.url, //仅为示例，并非真实的接口地址
        data,
        method,
        timeout,
        header: getReqHeader(),
        success(res) {
          resolve(res.data)
        },
        fail(err) {
          reject(err)
        }
      })
    })
  }
})

export default function request(config) {
  return requestCommon.request(config)
}
export function get(url, data, config) {
  return requestCommon.get(url, data, config)
}
export function post(url, data, config) {
  return requestCommon.post(url, data, config)
}
export function uploadFile(url, data, config) {
  return new Promise((resolve, rejcet) => {
    wx.uploadFile({
      url: baseUrl + url,
      filePath: data.file,
      name: data.name,
      header: getReqHeader(),
      success (res){
        resolve(res.data)
      },
      fail (err) {
        rejcet(err)
      }
    })
  })
}